<?php
namespace app\controller;

use app\BaseController;
use think\facade\Log;
use utils\Captcha;
use utils\UploadService;

class Index extends BaseController
{
    public function index()
    {
        phpinfo();
    }

    /**
     * 验证码
     * @return $this|\think\Response
     */
    public function captcha()
    {
        return (new Captcha())->create();
    }

    /**
     * 上传文件
     *
     * @return \think\Response
     */
    public function uploadFile()
    {
        try {
            $upload_type = config('filesystem.upload_type');
            $path = make_path('attach', 2, true);
            $upload = UploadService::init($upload_type);
            $res = $upload->to($path)->validate()->move('image');
            if ($res === false) {
                echo json_encode([
                    'err_code' => 400,
                    'err_msg' => $upload->getError()
                ]);exit;
            } else {
                $fileInfo = $upload->getUploadInfo();
                echo json_encode([
                    'err_code' => 200,
                    'src' => $res->fileName,
                    'err_msg' => '上传成功'
                ]);exit;
            }
        } catch (\Exception $e) {
            echo json_encode([
                'err_code' => 400,
                'err_msg' => $e->getMessage()
            ]);exit;
        }
    }

}
